package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.githang.statusbar.StatusBarCompat

import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.GroupOrderAdapter
import com.ht.medicaltreatment.bean.SghyMarshall
import com.ht.medicaltreatment.viewmodel.GroupOrderViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_group_order_layout.recyclerView

class GroupOrderListActivity : BaseActivity(), OnItemChildClickListener {
    private val model by viewModels<GroupOrderViewModel>()
    private val adapter = GroupOrderAdapter()
    private var uid = ""
    private var orderId = -1

    override fun getLayoutId() = R.layout.activity_group_order_layout

    override fun init() {
        StatusBarCompat.setStatusBarColor(this,getColor(R.color.white))
        setTitle("计划核验列表")

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        orderId = intent.getIntExtra("orderId",-1)
        initClick()
    }

    override fun onResume() {
        super.onResume()
        iniData()
    }

    private fun iniData() {
        if(orderId != -1){
            showProgress()
            model.orderList(orderId, onSuccess = {
                dismissProgress()
                adapter.setNewInstance(it)
            }, onError = {
                dismissProgress()
                showToast(it)
            })
        }else{
            startScan()
        }
    }

    override fun onRestart() {
        super.onRestart()
        if(uid != ""){
            showProgress()
            model.checkId(uid,{
                dismissProgress()
                adapter.setNewInstance(it)
            },{
                dismissProgress()
                showToast(it)
            })
        }
    }

    private fun initClick() {
        adapter.setOnItemChildClickListener(this)
    }

    fun startScan(){
        uid = intent.getStringExtra("uid")?:""
        showProgress()
        model.checkId(uid,{
            dismissProgress()
            adapter.setNewInstance(it)
        },{
            dismissProgress()
            showToast(it)
        })
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as SghyMarshall
        val intent = Intent(this,CheckGroupActivity::class.java)
        intent.putExtra("id",item.id)
        intent.putExtra("poistion",position)
        startActivity(intent)
    }
}
